#!/usr/bin/env python
# $Id: item_detail.py 2 2008-04-04 18:53:47Z benchline $

from page import page
import sys, time, urllib
import db 

class add(page):

	def __init__(self):
		page.__init__(self)
		self.headingstr = "Item Detail Report"
		self.db1 = db.db()
		self.upcdb1 = db.upcdb()

	def render(self):
		self.logic()
		self.encoding()
		self.html()

	def logic(self):
		if request.has_key("cancel"):
			raise HTTP_REDIRECTION, "menu.py"
		if request.has_key("submit") or request.has_key("upc"):
			self.upc = request["upc"][0]
			self.details = self.db1.get_upc_detail(self.upc)
		else:
			self.details = []

	def form(self):
		form = """
		<form name="add" method="GET" action="item_detail.py">
		<table style="text-align: left; width: 20%;" border="0" cellpadding="3" cellspacing="0">
		<tbody>
		<tr>
			<td>UPC</td>
			<td><input type=text name=upc></td>
		</tr>
		<tr>
			<td><input name=submit type=submit value="Submit"></td>
			<td><input type=submit name=cancel value="Menu"></td>
		</tr>
		<script>document.forms.add.upc.focus();</script>
		</tbody>
		</table>
		</form>
		""" 
		print form

	def show_detail(self):
		"""
		Show the detail
		"""
		print """<table border=1 cellpadding=3 cellspacing=0>
		<tr>
			<td>Count</td>
			<td>Description</td>
			<td>Amt</td>
			<td>Unit</td>
			<td>Purchase Date</td>
			<td>Type</td>
			<td>Shelf Life</td>
			<td>Goal Amt</td>
			<td>Goal Unit</td>
		</tr>
		"""
		i = 1
		for line in self.details:
			print "<tr><td>"+str(i)+"</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>" % line
			i += 1
		print "</table>"

	def message(self):
		if request.has_key("m"):
			message = request["m"][0]
			if message:
				print message

	def body(self):
		self.start_body()
		self.heading()
		self.form()
		self.message()
		self.show_detail()
		self.footer()
		self.end_body()

class runner:
	def _test(self):
		add().render()

	def _doctest(self):
		import doctest
		doctest.testmod()

if __name__ == "__main__":
	r = runner()
	getattr(r, "_"+sys.argv[1])()
else:
	add().render()
